<?php
/**
 * 首页图片轮播banner
 *
 * Author: Zeon
 * Date: 2018/1/23
 * Created by Panxsoft.
 */
namespace App\Http\Models;

use App\Observers\BosCleanObserver;
use App\Services\Utils\Bosable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;

/**
 * Class HomepageBanner
 *
 * @property string image
 *
 * @package App\Http\Models
 */
class HomepageBanner extends Model
{
    use Bosable;

    public $bos_columns = ['image'];  // bos 要删除的的字段

    protected $table = 'homepage_banners';

    protected static $unguarded = true;

    protected $bos_key_prefix = 'homepage-banners/';

    // Mutators BEGIN
    /**
     * @param UploadedFile|null|string $file
     */
    public function setImageAttribute($file = null)
    {
        if (! $file) return;

        if(is_file($file)) {
            $this->attributes['image'] = $this->uploadPostFile($file , $this->bos_key_prefix);
        }
    }

    /**
     * @return null|string
     */
    public function getImageAttribute()
    {
        if (! $this->attributes['image']) return null;

        return $this->getStaticUrl($this->attributes['image']);
    }
    // Mutators END

    /**
     *  注册删除bosKey事件绑定
     *
     */
    protected static function boot()
    {
        parent::boot();

        /*
         * 监听bos清理事件
         */
        self::observe((BosCleanObserver::class));
    }

}
